package controllers

import (
	beego "github.com/beego/beego/v2/server/web"
	"store/models"
)

type UserController struct {
	beego.Controller
}

type JsonRes struct {
	Code int         `json:"code"`
	Msg  string      `json:"msg"`
	Data interface{} `json:"data"`
}

func (c *UserController) Login() {
	c.Ctx.Output.Header("Access-Control-Allow-Origin", "*")
	username := c.GetString("username")
	password := c.GetString("password")
	var user models.User
	err := models.DB.Where("username=?", username).First(&user).Error
	if err != nil {
		c.Data["json"] = JsonRes{
			Code: 500,
			Msg:  "用户名错误，登录失败",
			Data: nil,
		}
		c.ServeJSON()
	} else {
		if password != user.Password {
			c.Data["json"] = JsonRes{
				Code: 500,
				Msg:  "用户密码错误，登录失败",
				Data: nil,
			}
			c.ServeJSON()
		} else {
			c.Data["json"] = JsonRes{
				Code: 200,
				Msg:  "登录成功",
				Data: nil,
			}
			c.ServeJSON()
		}
	}
}
